<?php
/**
 * This file is part of Notadd.
 *
 * @author        Hollydan <2642956839@qq.com>
 * @copyright (c) 2017, notadd.com
 * @datetime      17-11-22 下午5:27
 */

namespace Notadd\FullReduction\GraphQL\Mutations;


use GraphQL\Type\Definition\Type;
use Notadd\Foundation\GraphQL\Abstracts\Mutation;
use Notadd\Foundation\Validation\Rule;
use Notadd\FullReduction\Models\Active;

class ActiveDeleteMutation extends Mutation
{

    /**
     * @return array
     */
    public function args(): array
    {
        return [
            'id' => [
                'name' => 'id',
                'rules' => [
                    Rule::required(),
                    Rule::numeric(),
                    Rule::in('ext_full_reduction_actives'),
                ],
                'type' => Type::int(),
            ],
        ];
    }

    /**
     * @param $root
     * @param $args
     *
     * @return mixed
     */
    public function resolve($root, $args)
    {
        $active = Active::query()->find($args['id']);
        if ($active instanceof Active && $active->delete()) {
            return json_encode([
                'code' => 200,
                'message' => '删除活动信息成功',
            ]);
        } else {
            return json_encode([
                'code' => 200,
                'message' => '删除活动信息失败',
            ]);
        }
    }

    /**
     * @return \GraphQL\Type\Definition\ListOfType
     */
    public function type()
    {
        return $this->graphql->type('Active');
    }
}